const { DataTypes } = require('sequelize');
const sequelize = require('../config/database');

const Order = sequelize.define('Order', {
  id: {
    type: DataTypes.UUID,
    defaultValue: DataTypes.UUIDV4,
    primaryKey: true
  },
  order_number: {
    type: DataTypes.STRING,
    allowNull: false,
    unique: true,
    comment: '订单号'
  },
  user_id: {
    type: DataTypes.UUID,
    allowNull: false,
    comment: '用户ID'
  },
  type: {
    type: DataTypes.ENUM('recharge', 'purchase', 'service', 'other'),
    allowNull: false,
    comment: '订单类型'
  },
  amount: {
    type: DataTypes.DECIMAL(10, 2),
    allowNull: false,
    comment: '订单金额'
  },
  points: {
    type: DataTypes.INTEGER,
    defaultValue: 0,
    comment: '积分'
  },
  status: {
    type: DataTypes.ENUM(
      'pending',
      'paid',
      'processing',
      'completed',
      'cancelled',
      'refunded'
    ),
    defaultValue: 'pending',
    comment: '订单状态'
  },
  item_details: {
    type: DataTypes.JSON,
    defaultValue: [],
    comment: '订单项详情'
  },
  payment_method: {
    type: DataTypes.ENUM('wechat', 'alipay', 'bank', 'points', 'other'),
    allowNull: false,
    comment: '支付方式'
  },
  metadata: {
    type: DataTypes.JSON,
    defaultValue: {},
    comment: '元数据' 
  },
  completed_at: {
    type: DataTypes.DATE,
    comment: '完成时间'
  },
  cancelled_at: {
    type: DataTypes.DATE,
    comment: '取消时间'
  },
  refunded_at: {
    type: DataTypes.DATE,
    comment: '退款时间'
  }
}, {
  underscored: true,
  indexes: [
    {
      unique: true,
      fields: ['order_number']
    }
  ],
  comment: '订单信息表'
});

module.exports = Order; 